package com.hc.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hc.domain.Account;
import com.hc.service.IAccountService;
import com.hc.util.AuthToken;
import com.hc.util.IPHelper;
import com.hc.util.MD5Util;
import com.hc.util.OtherUtils;
import com.hc.util.PasswordUtil;
import com.hc.util.Redis;
import com.hc.util.TaskThreadProvider;

/**
 * @Description: 账户信息统一调用接口，实现应用账户分离，确保账户通用性
 * @author
 * @date 2016年10月20日 下午12:09:42
 * 
 */

@Controller
@RequestMapping(value = "Account")
public class AccountController extends BaseController {

	@Resource
	private IAccountService accountService;
	@Resource
	private TaskThreadProvider task;
	@Resource
	private Redis redis;
	/**
	 * 
	 * @Description： 用户登录
	 * 
	 * @param req
	 * @param resp
	 * @param value
	 * @return
	 * @author:
	 * @since: 2017年4月25日 下午2:39:16
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public JSON login(HttpServletRequest req, HttpServletResponse resp, @RequestParam(value="tel",required=true) String tel,
			@RequestParam(value="pass",required=true) String pass) {
		Account account = accountService.getAccountByTel(tel);
		if (account == null) {
			return Message(ERROR, "用户不存在或密码错误", null);
		}
		if (account.getStatus() != 1) {
			return Message(ERROR, "账户异常，请联系管理员", null);
		}
		if (!PasswordUtil.validPassword(account.getPass(), pass, account.getSalt())) {
			return Message(ERROR, "用户不存在或密码错误", null);
		}
		String ip = IPHelper.getIpAddr(req);
		String token = AuthToken.getToken(account.getUserId() + "", OtherUtils.getDateForMsec() + "",ip);
		// 更新用户登录记录
		task.getThreadProvider().submit(new Runnable() {
			@Override
			public void run() {
				account.setLastLoginIp(ip);
				account.setLastLoginDate(OtherUtils.getCurrentDate());
				account.setLoginCount(account.getLoginCount() + 1);
				redis.setEx("user" + account.getUserId(), 60 * 60 * 24 * 30, token);
				accountService.updateOrSaveAccount(account);
			}
		});
		JSONObject userInfo = new JSONObject();
		userInfo.put("token", token);
		userInfo.put("userId", account.getUserId());
		return Message(SUCCESS, "登录成功", userInfo);
	}
	/**
	 * 
	 * @Description： 用户注册
	 * @param resp
	 * @param tel
	 * @param pass
	 * @param code
	 * @return
	 * @author:  
	 * @since: 2017年5月16日 下午12:20:19
	 */
	@ResponseBody
	@RequestMapping(value = "/register")
	public JSON register(HttpServletResponse resp,  @RequestParam(value="tel",required=true) String tel,
			@RequestParam(value="pass",required=true) String pass,
			@RequestParam(value="msgCode",required=true) String code) throws Exception{
		Account user = accountService.getAccountByTel(tel);
		if (user != null) {
			return Message(ERROR, "该手机号已注册", null);
		}
		// TODO:短信验证码
		// String msgCode = redis.get("msg"+tel);
		String msgCode = "123456";
		if (isEmpty(msgCode)) {
			return Message(ERROR, "验证码已失效", null);
		}
		if (!msgCode.equals(code)) {
			return Message(ERROR, "验证码错误，请核对验证码", null);
		}
		if (OtherUtils.isNumber(pass)) {
			return Message(ERROR, "密码大于8位，且不能全为数字", null);
		}
		if (OtherUtils.getWordCount(pass) < 8) {
			return Message(ERROR, "密码虚大于8位，且不能全为数字", null);
		}
		user = new Account();
		String salt = MD5Util.getStringHash(tel);
		user.setSalt(salt);
		user.setTel(tel);
		user.setPass(PasswordUtil.MD5Generator(pass, salt));
		user.setAddDate(OtherUtils.getCurrentDate());
		accountService.updateOrSaveAccount(user);
		return Message(SUCCESS, "账号注册成功", null);
	}

	@ResponseBody
	@RequestMapping(value = "/modifyPass")
	public JSON modifyPass(HttpServletResponse resp, @RequestParam(value="tel",required=true) String tel, @RequestParam(value="pass",required=true) String pass,
			@RequestParam(value="msgCode",required=true) String code) throws Exception{
		Account user = accountService.getAccountByTel(tel);
		if (user == null) {
			return Message(ERROR, "为查找到该账号", null);
		}
		// TODO:短信验证码
		// String msgCode = redis.get("msg"+tel);
		String msgCode = "123456";
		if (isEmpty(msgCode)) {
			return Message(ERROR, "验证码已失效", null);
		}
		if (!msgCode.equals(code)) {
			return Message(ERROR, "验证码错误，请核对验证码", null);
		}
		if (OtherUtils.isNumber(pass)) {
			return Message(ERROR, "密码虚大于8位，且不能全为数字", null);
		}
		if (OtherUtils.getWordCount(pass) < 8) {
			return Message(ERROR, "密码虚大于8位，且不能全为数字", null);
		}
		String salt = MD5Util.getStringHash(tel);
		user.setSalt(salt);
		user.setPass(PasswordUtil.MD5Generator(pass, salt));
		accountService.updateOrSaveAccount(user);
		return Message(SUCCESS, "密码修改成功", null);
	}

}
